﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MOJsdk;

namespace MOJ
{
    /// <summary>
    /// Interaction logic for Control_Organize.xaml
    /// </summary>
    public partial class Control_Organize : UserControl
    {
        private MojSystem _system;

        public Control_Organize()
        {
            InitializeComponent();
            _system = GlobalVars.TheHost.SystemVars;
            INIStyle();
            this.Loaded += OnLoad;
            _btn_save.Click += SaveVars;
        }

        private void INIStyle()
        {
            _btn_save.Content = MOJLocalization.Localize("Save");

            _lbl_diskid.Content = MOJLocalization.Localize("Disk") + ": ";
            _lbl_file.Content = MOJLocalization.Localize("File Name") + ": ";
            _lbl_folder.Content = MOJLocalization.Localize("Folder Name") + ": ";
            _lbl_num.Content = MOJLocalization.Localize("File Numeration") + ": ";

            _chk_MojOrgKeepFileDisk.Content = MOJLocalization.Localize("Keep file disk");
            _chk_ShowFileNumerOnlyOnMultiMovies.Content = MOJLocalization.Localize("Show file number only on multi movies");

            _Grid.ApplyStyle("0101140");

            _lbl_diskid.ApplyStyle("0101141");
            _lbl_file.ApplyStyle("0101142");
            _lbl_folder.ApplyStyle("0101143");
            _lbl_num.ApplyStyle("0101144");

            _txt_file.ApplyStyle("0101145");
            _txt_folder.ApplyStyle("0101146");
            _txt_num.ApplyStyle("0101147");

            _chk_ShowFileNumerOnlyOnMultiMovies.ApplyStyle("0101148");
            _chk_MojOrgKeepFileDisk.ApplyStyle("0101149");

            _cmb_diskid.ApplyStyle("0101150");
            _btn_save.ApplyStyle("0101151");
        }
        private void OnLoad(object sender, RoutedEventArgs e)
        {
            MOJOrganizeRules r = new MOJOrganizeRules();

            _txt_file.Text = r.FileName;
            _txt_folder.Text = r.FolderName;
            _txt_num.Text = r.FileNumber;

            _chk_MojOrgKeepFileDisk.IsChecked = r.KeepFileDisk;
            _chk_ShowFileNumerOnlyOnMultiMovies.IsChecked = r.ShowFileNumberOnlyOnMultiMovies;

            _cmb_diskid.Items.Clear();
            foreach (var v in GlobalVars.TheHost.DrivePool)
                _cmb_diskid.Items.Add(v.Value);

            if (GlobalVars.TheHost.DrivePool.ContainsKey(r.DiskID))
            {
                for (int i = 0; i < _cmb_diskid.Items.Count; i++)
                {
                    if (_cmb_diskid.Items[i].ToString() == GlobalVars.TheHost.DrivePool[r.DiskID])
                    {
                        _cmb_diskid.SelectedIndex = i;
                        return;
                    }
                }
            }
            else
                _cmb_diskid.SelectedIndex = -1;
                
        }

        private void SaveVars(object sender, RoutedEventArgs e)
        {
            _system.SetVar("MojOrgFolderName", _txt_folder.Text);
            _system.SetVar("MojOrgFileName", _txt_file.Text);
            _system.SetVar("MojOrgNumber", _txt_num.Text);
            _system.SetVar("MojOrgKeepFileDisk", _chk_MojOrgKeepFileDisk.IsChecked.Value.ToString());
            _system.SetVar("MojOrgShowOnlyOnMulti", _chk_ShowFileNumerOnlyOnMultiMovies.IsChecked.Value.ToString());

            int i = _cmb_diskid.SelectedIndex;
            if (i != -1)
            {
                string id = _cmb_diskid.Items[i].ToString();
                foreach (var v in GlobalVars.TheHost.DrivePool)
                    if (v.Value == id)
                    {
                        _system.SetVar("MojOrgDiskID", v.Key);
                        return;
                    }
            }
            
            _system.SetVar("MojOrgDiskID", "0");
        }
    }
}
